package cn.csust.net2.manager.client.panel;

import java.util.List;

import cn.csust.net2.manager.client.ux.ServiceCallback;
import cn.csust.net2.manager.shared.po.Academy;
import cn.csust.net2.manager.shared.po.FileNotification;
import cn.csust.net2.manager.shared.service.FileNoticServiceAsync;
import cn.csust.net2.manager.shared.util.Constant;
import cn.csust.net2.manager.shared.util.Factory;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.CheckBox;
import com.extjs.gxt.ui.client.widget.form.CheckBoxGroup;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.FileUploadField;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.FormPanel.Encoding;
import com.extjs.gxt.ui.client.widget.form.FormPanel.Method;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.google.gwt.core.client.GWT;

public class AddFileNoticWindow extends Window{
	FileNoticServiceAsync service;
	FileNotification fileNotification;
	FormPanel panel = new FormPanel();  
	public AddFileNoticWindow(FileNoticServiceAsync service){
		this.service = service;
		init();
	}
	public void init(){
		this.setTitle("发送文件通知");
		this.setSize(500, 500);	
		panel.setHeaderVisible(false);  
		panel.setFrame(true);  
		this.setLayout(new FitLayout());
		panel.setAction(GWT.getModuleBaseURL() + "FileNotification");
		panel.setEncoding(Encoding.MULTIPART);  
		panel.setMethod(Method.POST);  
		panel.setLayoutOnChange(true);
		panel.setButtonAlign(HorizontalAlignment.CENTER);  
		panel.setWidth(500);  
		panel.setHeight(500);  
		TextField<String> name = new TextField<String>();  
		name.setName("name");
		name.setFieldLabel("标题");  
		panel.add(name);  
		TextField<String> fileName = new TextField<String>();  
		fileName.setFieldLabel("文件名格式");  
		fileName.setName("needName");
		panel.add(fileName);  
		DateField date = Factory.createDateField();
	//	DateField date = new DateField();  
	    date.setFieldLabel("截止日期");  
	    date.setData("text", "Enter your birthday");  
	    date.setName("endDate");
	    panel.add(date,  new FormData("40%"));  
	   
	    service.findAllAcademy(new ServiceCallback<List<Academy>>() {
			
			@Override
			public void onSuccess(List<Academy> result) {
				  CheckBoxGroup checkGroup = new CheckBoxGroup();  
					checkGroup.setFieldLabel("接受单位");
					checkGroup.setName("academys");
					
				for(Academy a :result){
					CheckBox check2 = new CheckBox();  
					check2.setBoxLabel(a.getAcademyName());  
					check2.setValue(true); 
					check2.setName(Constant.FILE_UPLOAD_PARA_ITEM+a.getAcademyID());
					checkGroup.add(check2); 
				}
				panel.add(checkGroup, new FormData("100%"));
				 FileUploadField file = new FileUploadField();  
					file.setAllowBlank(false);  
					file.setName("file");  
					file.setFieldLabel("文件");  
					panel.add(file);  
				
			}
		});
	    TextArea description = new TextArea();  
	    description.setPreventScrollbars(true); 
	    description.setName("txtContent");
	    description.setFieldLabel("内容");  
	    panel.add(description,  new FormData("100%"));  
	   
		Button btn = new Button("Reset");  
		    btn.addSelectionListener(new SelectionListener<ButtonEvent>() {  
		      @Override  
		      public void componentSelected(ButtonEvent ce) {  
		        panel.reset();  
		      }  
		    });  
		    panel.addButton(btn);  
		  
		    btn = new Button("Submit");  
		    btn.addSelectionListener(new SelectionListener<ButtonEvent>() {  
		      @Override  
		      public void componentSelected(ButtonEvent ce) {  
		        if (!panel.isValid()) {  
		          return;  
		        }  
		        panel.setData("abc", "cccccccccccccccc");
		        panel.submit();
		        MessageBox.info("Action", "You file was uploaded", null);  
		      }  
		    });  
		    panel.addButton(btn);  
		   
		    add(panel);  
		this.show();
	}
}
